﻿namespace Hidistro.UI.Web.pay
{
    using Hidistro.Core;
    using Hidistro.Entities.Orders;
    using Hidistro.Entities.Promotions;
    using Hidistro.Entities.Sales;
    using Hidistro.Membership.Context;
    using Hidistro.Membership.Core;
    using Hidistro.Membership.Core.Enums;
    using Hidistro.Messages;
    using Hidistro.SaleSystem.Member;
    using Hidistro.SaleSystem.Shopping;
    using Hishop.Plugins;
    using System;
    using System.Collections.Specialized;
    using System.Globalization;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class wap_alipay_return_url : Page
    {
        protected decimal Amount;
        protected string Gateway;
        protected PaymentNotify Notify;
        protected OrderInfo Orders;
        protected JSShop.Model.OrderInfo Order;
        protected string OrderId;
        private Literal litMessage;




        private void FinishOrder()
        {
            if (this.Order.OrderStatus == JSShop.Enum.OrderStatus.Finished)
            {
                base.Response.Write(string.Format("<p style=\"font-size:16px;\">恭喜您，订单已成功完成支付：{0}</br>支付金额：{1}</p>", this.OrderId, this.Amount.ToString("F")));
            }
            else if (this.Orders.CheckAction(OrderActions.BUYER_CONFIRM_GOODS) && TradeHelper.ConfirmOrderFinish(this.Order))
            {
                base.Response.Write(string.Format("<p style=\"font-size:16px;\">恭喜您，订单已成功完成支付：{0}</br>支付金额：{1}</p>", this.OrderId, this.Amount.ToString("F")));
            }
            else
            {
                base.Response.Write(string.Format("<p style=\"font-size:16px;color:#ff0000;\">订单支付已成功，但是系统在处理过程中遇到问题，请联系管理员</br>支付金额：{0}</p>", this.Amount.ToString("F")));
            }
        }

        private void Notify_Finished(object sender, FinishedEventArgs e)
        {
            if (e.IsMedTrade)
            {
                this.FinishOrder();
            }
            else
            {
                this.UserPayOrder();
            }
        }

        private void Notify_NotifyVerifyFaild(object sender, EventArgs e)
        {
            base.Response.Write("<p style=\"font-size:16px;\">签名验证失败，可能支付密钥已经被修改</p>");
        }

        private void Notify_Payment(object sender, EventArgs e)
        {
            this.UserPayOrder();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Response.Write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"/>");
            NameValueCollection values2 = new NameValueCollection();
            values2.Add(this.Page.Request.Form);
            values2.Add(this.Page.Request.QueryString);
            NameValueCollection parameters = values2;
            String[] myStrArr = new String[parameters.Count];
            values2.CopyTo(myStrArr, 0);
            this.Gateway = "wp_bankunionrequest";
            //this.Notify = PaymentNotify.CreateInstance(this.Gateway, parameters);
            this.OrderId = myStrArr[0];
            //this.Order = ShoppingProcessor.GetOrderInfo(this.OrderId);
            this.Order = new Juse.Interface.OrderInterface().GetOrderInfo(this.OrderId);
            if (this.Order == null)
            {
                base.Response.Write("<p style=\"font-size:16px;\">找不到对应的订单，你付款的订单可能已经被删除</p>订单状态:" + Order.OrderStatus);


            }
            else
            {
                //Order.OrderStatus = JSShop.Enum.OrderStatus.BuyerAlreadyPaid;
                Order.IsAlreadyPay = 1;
                this.UserPayOrder();
                //this.Amount = this.Notify.GetOrderAmount();
                //if (this.Amount <= 0M)
                //{
                //    this.Amount = this.Order.GetTotal();
                //}
                //this.Order.GatewayOrderId = this.Notify.GetGatewayOrderId();
                //PaymentModeInfo paymentMode = ShoppingProcessor.GetPaymentMode("wp_bankunionrequest");
                //if (paymentMode == null)
                //{
                //    base.Response.Write("<p style=\"font-size:16px;\">找不到对应的支付方式，该支付方式可能已经被删除</p>");
                //}
                //else
                //{
                //    this.Notify.Finished += new EventHandler<FinishedEventArgs>(this.Notify_Finished);
                //    this.Notify.NotifyVerifyFaild += new EventHandler(this.Notify_NotifyVerifyFaild);
                //    this.Notify.Payment += new EventHandler(this.Notify_Payment);
                //    this.Notify.VerifyNotify(0x7530, HiCryptographer.Decrypt(paymentMode.Settings));
                //}
            }
        }

        private void UserPayOrder()
        {
            //if (this.Order.OrderStatus == JSShop.Enum.OrderStatus.BuyerAlreadyPaid)
                if (this.Order.IsAlreadyPay == 1)
            {
                Amount = Order.GetAmount();
                TradeHelper.UserPayOrder(this.Order, true);
                base.Response.Write(string.Format("<p style=\"font-size:16px;\">恭喜您，订单已成功完成支付：{0}</br>支付金额：{1}</p>", this.OrderId, this.Amount.ToString("F")));
            }
            else
            {
                int maxCount = 0;
                int orderCount = 0;
                int groupBuyOerderNumber = 0;
                if (this.Order.GroupBuyId > 0)
                {
                    GroupBuyInfo groupBuy = TradeHelper.GetGroupBuy(this.Order.GroupBuyId);
                    if ((groupBuy == null) || (groupBuy.Status != GroupBuyStatus.UnderWay))
                    {
                        base.Response.Write("<p style=\"font-size:16px;\">订单为团购订单，团购活动已结束，支付失败</p>");
                        return;
                    }
                    orderCount = TradeHelper.GetOrderCount(this.Order.GroupBuyId);
                    groupBuyOerderNumber = this.Orders.GetGroupBuyOerderNumber();
                    maxCount = groupBuy.MaxCount;
                    if (maxCount < (orderCount + groupBuyOerderNumber))
                    {
                        base.Response.Write("<p style=\"font-size:16px;\">订单为团购订单，订购数量超过订购总数，支付失败</p>");
                        return;
                    }
                }
                //if (this.Orders.CheckAction(OrderActions.BUYER_PAY) && TradeHelper.UserPayOrder(this.Order, false))
                if (TradeHelper.UserPayOrder(this.Order, false))
                {
                    DebitNoteInfo note = new DebitNoteInfo
                    {
                        NoteId = Globals.GetGenerateId(),
                        OrderId = this.Order.OrderId,
                        Operator = this.Order.Username,
                        Remark = "客户订单在线支付成功"
                    };
                    TradeHelper.SaveDebitNote(note);
                    if ((this.Order.GroupBuyId > 0) && (maxCount == (orderCount + groupBuyOerderNumber)))
                    {
                        TradeHelper.SetGroupBuyEndUntreated(this.Order.GroupBuyId);
                    }
                    if ((this.Order.UserId != 0) && (this.Order.UserId != 0x44c))
                    {
                        IUser user = Users.GetUser(this.Order.UserId);
                        if ((user != null) && (user.UserRole == UserRole.Member))
                        {
                            Messenger.OrderPayment(user, this.Order.OrderId, this.Order.GetTotal());
                        }
                    }
                    this.Orders.OnPayment();
                    base.Response.Write(string.Format("<p style=\"font-size:16px;\">恭喜您，订单已成功完成支付：{0}</br>支付金额：{1}</p>", this.OrderId, this.Amount.ToString("F")));
                }
                else
                {
                    base.Response.Write(string.Format("<p style=\"font-size:16px;color:#ff0000;\">订单支付已成功，但是系统在处理过程中遇到问题，请联系管理员</br>支付金额：{0}</p>", this.Amount.ToString("F")));
                }
            }
        }

    }
}

